<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<jsp:include page="../../common/assets_header.jsp" />
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<script type="text/javascript"
	src="<%=request.getContextPath()%>/plugins/zTree/jquery.ztree.all.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/plugins/zTree/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/plugins/zTree/jquery.ztree.excheck.js"></script>
<link
	href="<%=request.getContextPath()%>/plugins/zTree/zTreeStyle/zTreeStyle.css"
	rel="stylesheet" type="text/css" />

<link
	href="<%=request.getContextPath()%>/plugins/zTree/metroStyle/metroStyle.css"
	rel="stylesheet" type="text/css" />
<script>
jQuery(document).ready(function() {    
	var tree=${list};
	getTree(tree);
})
function getTree(tree){
	var treeNodes =tree;
	var setting={
			   check: {
			        enable: true,
					chkboxType: { "Y": "s", "N": "s" }
			    },
			data:{
			key:{
			checked: "isChecked",
			name:"name",
			title:"name"
			},
			simpleData:{
			enable:true,
			idKey:"PId",
			pIdKey:"parentId"
			}
			}
		};
	
	 $.fn.zTree.init($("#tree"), setting, treeNodes);
	 var treeObj = $.fn.zTree.getZTreeObj("tree");
	 var nodes = treeObj.getNodesByParam("isChecked", true, null);
	 if(nodes!=null){
		 for (var i=0, l=nodes.length; i < l; i++) {
				treeObj.checkNode(nodes[i], true, true);
			}
	 }
	 treeObj.expandAll(true);
}
function saveOrEdit(){
	
	var treeObj = $.fn.zTree.getZTreeObj("tree");
	var listPid="";
	var sNodes = treeObj.getCheckedNodes(true);
	if (sNodes.length > 0) {
		sNodes.forEach(function(e){ 
		   var pId=e.PId;
		   if(pId.indexOf("pur_")!=-1){
			   listPid+=pId.replace("pur_","")+"^";
		   }
		})  
	}
	$("#listPid").val(listPid);
	
	$("#categoryPurview").attr("action","<%=request.getContextPath()%>/prod/purview/save" );
	$("#categoryPurview").submit();

	}
	
function back() {
	$("#categoryPurview").attr("action","<%=request.getContextPath()%>/prod/category/list" );
	$("#categoryPurview").submit();

}
</script>

</head>
<body>

	<!-- content -->
	<div class="page-content">
		<div class="page-header">
			<h1>
				<small> ${prodCategory.categoryName} --产品类别权限 </small>
			</h1>
		</div>
		<div class="row">
			<c:if test="${success}">
				<div class="alert alert-block alert-success">
					<button class="close" data-dismiss="alert">
						<i class="icon-remove"></i>
					</button>
					<strong><spring:message code="success"></spring:message>!</strong>
					<spring:message code="save success"></spring:message>
					！
				</div>
			</c:if>

			<c:if test="${success==false}">
				<div class="alert alert-danger">
					<button type="button" class="close" data-dismiss="alert">
						<i class="icon-remove"></i>
					</button>
					<strong> <i class="icon-remove"></i> <strong><spring:message
								code="failure"></spring:message>!</strong> ${message}
					</strong>
				</div>
			</c:if>
			<div class="col-xs-12">
				<form id="categoryPurview" class="form-horizontal">
					<input type="hidden" id="id" name="id" value="${prodCategory.prodCategoryId}">
					<input type="hidden" id="listPid" name="listPid" value="">

					<div class="form-group">

						<div class="controls">
							<div id="tree" class="ztree" >
							</div>
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="form-actions center">

						<button type="button" class="btn btn-xs btn-primary"
							onclick="saveOrEdit();" class="btn">
							<spring:message code="保存"></spring:message>
						</button>

						<button type="button" class="btn btn-xs btn-primary"
							onclick="back()" class="btn">
							<spring:message code="btn.common.back"></spring:message>
						</button>

					</div>

				</form>
			</div>
		</div>
		<!-- BEGIN PAGE CONTAINER-->
	</div>
	<!-- content end -->

</body>